<!-- eslint-disable vue/no-parsing-error -->
<template>
  <view v-if="shopList.length > 0" class="shop-container">
    <view v-for="shop in shopList" :key="shop.id" class="shop">
      <view class="shop__title" @click="toShopDetails(shop)">
        <view class="shop__title-image">
          <!-- #ifndef H5 -->
          <image
            :src="cropImg(shop.logo, getCropPx(112), getCropPx(112))"
            mode="aspectFit"
            style="width: 100%; height: 100%"
            :lazy-load="true"
          ></image>
          <!-- #endif -->
          <!-- #ifdef H5 -->
          <lazy-load>
            <img :src="cropImg(shop.logo, getCropPx(112), getCropPx(112))" mode="aspectFit" style="width: 100%; height: 100%" />
          </lazy-load>
          <!-- #endif -->
        </view>
        <view style="flex: 1; padding-left: 16rpx">
          <view style="margin-bottom: 2rpx; display: flex; align-items: center; height: 44rpx; margin-bottom: 4rpx">
            <text
              v-if="shop.shopType && shop.shopType !== 'ORDINARY'"
              class="shop__tag"
              :style="{ background: shop.shopType === 'SELF_OWNED' ? '#fd0505' : '#7728f5' }"
            >
              {{ shopType[shop.shopType] }}
            </text>
            <text class="shop__title--name">
              {{ shop.name }}
            </text>
          </view>
          <view style="display: flex; font-size: 24rpx; color: #666">
            <text>月销 {{ shop.salesVolume }}</text>
            <template v-if="getterLocation?.coordinates && decorationProperties.showStyle === 'is-style-four'">
              <text v-if="getterLocation?.coordinates?.length" style="margin: 0 25rpx">距离{{ new Decimal(shop.distance).toFixed(2) || 0 }}㎞</text>
              <text style="margin: 0 25rpx">起送￥{{ shop.initialDeliveryCharge || 0 }}</text>
            </template>
          </view>
          <!-- 优惠券 -->
          <view class="coupon-list">
            <view v-for="(item, index) in shop.couponList" :key="index" :data-index="index" class="coupon-button">{{ item.sourceDesc }}</view>
          </view>
        </view>
      </view>
      <scroll-view :scroll-x="true">
        <view class="goods-list">
          <Goods v-for="item in shop.goodsList" :key="item.id" class="goods-list__item" :goods-item="item" :shop-id="shop.id" />
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { formData } from '../shop-goods-default.vue'
import Goods from './components/goods.vue'
import { doGetCommodityByShop, doGetShopListByDistance, doGetShopListBySalesVolume } from '@/apis/o2oshop'
import { ApiSearchShop } from './types'
import { cropImg, getCropPx } from '@/utils'
import Decimal from 'decimal.js'
import { useLocationStore } from '@/store/modules/location'
import { storeToRefs } from 'pinia'

const { divTenThousand } = useConvert()
interface IProps {
  decorationProperties: typeof formData
}
const props = withDefaults(defineProps<IProps>(), {
  decorationProperties: () => formData,
})
const { getterLocation } = storeToRefs(useLocationStore())

const shopList = ref<ApiSearchShop[]>([])

const shopType = {
  SELF_OWNED: '自营',
  PREFERRED: '优选',
}

const initialShopList = async () => {
  if (props.decorationProperties.showStyle === 'is-style-four') {
    // 按距离
    const requestPromise = doGetShopListByDistance({
      sortAsc: true,
      latitude: getterLocation.value?.coordinates?.[1],
      longitude: getterLocation.value?.coordinates?.[0],
    })
    const result = await requestPromise
    shopList.value = result.data || []
  } else {
    // 按销量
    const requestPromise = doGetShopListBySalesVolume({ sortAsc: false })
    const result = await requestPromise
    shopList.value = result.data || []
  }
}

const toShopDetails = (shop: ApiSearchShop) => {
  uni.navigateTo({
    url: `/basePackage/pages/merchant/Index?shopId=${shop.id}`,
  })
}
/**
 * 获取商品列表
 */
async function getGoodList() {
  const shopIds = shopList.value.map((item) => item.id)
  const result = await doGetCommodityByShop(shopIds)
  if (result.code !== 200) {
    return uni.showToast({ icon: 'none', title: `${result.msg || '获取商品列表失败'}` })
  }
  const resultData = result.data
  resultData.forEach((shopResult: any) => {
    const shopId = shopResult.shopId
    const index = shopList.value.findIndex((item) => item.id === shopId)
    const goodsList = (shopResult?.productSaleVolumes || []).map((product: any) => ({
      id: product.productId,
      name: product.productName,
      logo: product.pic,
      price: divTenThousand(product.productPrice) as unknown as string,
    }))
    if (index > -1) {
      shopList.value[index].goodsList = goodsList
    }
  })
}
const initialData = async () => {
  await initialShopList()
  getGoodList()
}
initialData()
const couponId = ref('') // 当前行点击的标识
const routecss = ref('transform: rotate(90deg)')
// 展开优惠券
const moreCoupon = (shop: ApiSearchShop) => {
  console.log(couponId.value)
  if (couponId.value === shop.id) {
    couponId.value = ''
    routecss.value = 'transform: rotate(90deg)'
  } else {
    couponId.value = shop.id
    routecss.value = 'transform: rotate(-90deg)'
  }
}
</script>

<style scoped lang="scss">
@include b(shop-container) {
  padding: 0 20rpx 20rpx;
  min-height: 524rpx;
  background-color: #f6f6f6;
}
@include b(shop) {
  font-size: 28rpx;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  background-color: #fff;

  @include e(tag) {
    padding: 0 10rpx;
    height: 36rpx;
    background-color: #fd0505;
    color: #fff;
    border-radius: 8rpx;
    font-size: 24rpx;
    line-height: 36rpx;
    flex-shrink: 0;
    margin-right: 12rpx;
  }

  @include e(title) {
    padding: 32rpx 20rpx 0;
    display: flex;

    @include m(name) {
      color: #333;
      font-size: 32rpx;
      font-weight: 600;
      max-width: 500rpx;
      @include utils-ellipsis(1);
    }
  }

  @include e(title-image) {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 120rpx;
    height: 120rpx;
    border-radius: 8rpx;
    overflow: hidden;
    flex-shrink: 0;
  }
}

@include b(active) {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 52rpx;
  width: 725rpx;
  border: 1px solid #fcfcfc;
  border-radius: 0 0 16rpx 16rpx;
  background-color: #fff;
  border-top-color: transparent;
}

.point {
  width: 14rpx;
  height: 14rpx;
  background: #ff3a3a;
  border-radius: 7rpx;
  opacity: 0.4;
  margin-left: 20rpx;
}

.current {
  opacity: 1;
}
.more-icon {
  position: absolute;
  right: 10%;
  transform: rotate(90deg);
  transition: all 0.2s;
}
.coupon-list {
  display: flex;
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  max-width: 290px;
}
@keyframes fadeInDown_h2 {
  0% {
    -webkit-transform: translate3d(0, -100%, 0);
    transform: translate3d(0, -100%, 0);
  }

  to {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
  }
}

.coupon-button {
  position: relative;
  border: 0.4px solid rgb(245, 67, 25);
  border-radius: 2px;
  background: rgb(255, 255, 255);
  padding: 2rpx 8rpx;
  overflow: hidden;
  display: inline-block;
  font-size: 20rpx;
  margin-right: 20rpx;
  margin-top: 4rpx;
  color: #f54319;
}

@include b(goods-list) {
  padding: 20rpx 0;
  white-space: nowrap;
  width: 100%;
  min-height: 309rpx;

  @include e(item) {
    display: inline-block;
  }
}
</style>
